package com.xin.bi.controller;


import com.xin.bi.common.BaseResponse;
import com.xin.bi.constant.AiConstant;
import com.xin.bi.manager.AiManager;
import com.xin.bi.manager.RedisLimterManager;
import com.xin.bi.model.vo.BiResponse;
import com.xin.bi.model.vo.LoginUserVO;
import com.xin.bi.mq.BiMessageProducer;
import com.xin.bi.service.ChartService;
import com.xin.bi.service.UserService;
import com.xin.bi.utils.ExcelUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
public class ChartController {
    @Resource
    private UserService userService;

    @Resource
    private ChartService chartService;

    @Resource
    private RedisLimterManager redisLimterManager;

    @Resource
    private AiManager aiManager;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Resource
    private BiMessageProducer biMessageProducer;

    /**
     * 根据ai生成图表    同步操作
     * @param genChartByAiRequest
     * @param multipartFile
     * @param httpServletRequest
     * @return
     */
    // 这里josn和文件上传用冲突，解决方案，传一个json字符串，自己手动转成想要数据
    @PostMapping("/genchart")
    public BaseResponse<BiResponse> genChar(
            @RequestParam("genChartByAiRequest") String genChartByAiRequest,
            @RequestParam("file") MultipartFile multipartFile,
            HttpServletRequest httpServletRequest) {
        // 流量限制
        LoginUserVO loginUserVO = userService.getLoginUserVO(httpServletRequest);
        final String LIMIT_PREFIX = "genchart";
        redisLimterManager.doRateLimit(LIMIT_PREFIX + loginUserVO.getId());
        // excel数据转成csv,并且拼接好发给ai数据
        String csvData = ExcelUtils.excelTOCSV(multipartFile);
        String sendAiMessage = chartService.jointSendAiMessage(genChartByAiRequest, csvData);
        String resAIData = aiManager.doChat(AiConstant.BI_MODEL_ID, sendAiMessage);
        BiResponse biResponse = chartService.genChart(genChartByAiRequest, csvData, resAIData, loginUserVO.getId());
        return new BaseResponse<>(true,biResponse,"根据AI生成图标成功");
    }

    /**
     * 根据ai生成图表 异步操作
     * @param genChartByAiRequest
     * @param multipartFile
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/genchartasync")
    public BaseResponse<Long> genchartasync(@RequestParam("genChartByAiRequest") String genChartByAiRequest,
                                            @RequestParam("file") MultipartFile multipartFile,
                                            HttpServletRequest httpServletRequest){
        // 流量限制
        LoginUserVO loginUserVO = userService.getLoginUserVO(httpServletRequest);
        final String LIMIT_PREFIX = "genchart";
        redisLimterManager.doRateLimit(LIMIT_PREFIX + loginUserVO.getId());
        // excel数据转成csv,并且拼接好发给ai数据
        String csvData  = ExcelUtils.excelTOCSV(multipartFile);
        // 预生成图表信息
        Long id = chartService.preGenChart(genChartByAiRequest, csvData, loginUserVO.getId());
        String sendAiMessage = chartService.jointSendAiMessage(genChartByAiRequest, csvData);
        // 异步调用AI
        CompletableFuture.runAsync(()->{
            chartService.genChartAsync(id,sendAiMessage);
        },threadPoolExecutor);
        return new BaseResponse<>(true,id,"根据AI生成图标成功");

    }

    /**
     * 使mq进行优化
     * @param genChartByAiRequest
     * @param multipartFile
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/genchartasyncmq")
    public BaseResponse<Long> genchartasyncmq(@RequestParam("genChartByAiRequest") String genChartByAiRequest,
                                            @RequestParam("file") MultipartFile multipartFile,
                                            HttpServletRequest httpServletRequest){
        // 流量限制
        LoginUserVO loginUserVO = userService.getLoginUserVO(httpServletRequest);
        final String LIMIT_PREFIX = "genchart";
        redisLimterManager.doRateLimit(LIMIT_PREFIX + loginUserVO.getId());
        // excel数据转成csv,并且拼接好发给ai数据
        String csvData = ExcelUtils.excelTOCSV(multipartFile);
        // 预生成图表信息
        Long id = chartService.preGenChart(genChartByAiRequest, csvData, loginUserVO.getId());

        String sendAiMessage = chartService.jointSendAiMessage(genChartByAiRequest, csvData);
        biMessageProducer.sendMessage(String.valueOf(id));
        return new BaseResponse<>(true,id,"根据AI生成图标成功");

    }
}
